<?php

	session_start();
	require_once 'includes/eveclass.php';
	$eve = new Eve();
	$eve->RequestTrust("Tu_es_einfach_Soldat");

	require_once 'includes/connect.php';
?>
<html>
<head>
	<title>TNP's AssignTool</title>
</head>
<body>
<?php
	$db = mysql_connect(chop($host), chop($username), chop($pwd)) or die ('Keine DB Verbindung : ' . mysql_error());
	mysql_select_db($database, $db) or die ('Kann nicht auf DB zugreifen : ' . mysql_error());

	$eve = new Eve();

	$eve->RequestTrust("Tu es einfach, Soldat!");

	if(empty($eve->UserName)) {
		echo ("Please trust this site in your eve client.<br>
				to do this klick OPTIONS -> TRUSTED SITES -> TRUST THIS SITE<br>
				and refresh the page.");
		exit;
	}

	if(isset($_GET["fleet"])) {
		$_SESSION["fleet"] = $_GET["fleet"];
		echo('<meta http-equiv="refresh" content="0; URL=http://unternetz.de/eve/">');
		exit;
	}

	if(!isset($_SESSION["fleet"])) {
		$_SESSION["fleet"] = time();
		echo("<h1>New Fleet Created!</h1>\n");
	}

	$_SESSION["fleet"] = (int)$_SESSION["fleet"];

	$is_admin = false;
	if (in_array($eve->UserName, array(
			'TNPklon',
			'Raist Varis',
			'Serieana',
			'Giselee',
			'SpaceyX',
			'Madzero',
			'MinmatarCitizen 675329',
	''
		))) {
		$is_admin = true;
	}

	function assignZealot($name, $toname, $db){
		$sql = 'UPDATE players SET assigned = "'.$toname.'" WHERE name LIKE "'.$name.'"';
		$result = mysql_query($sql, $db);
	}

	function assign($db){

		$guardians = array();
		$zealots = array();

		$sql = 'SELECT * FROM players WHERE type LIKE "Guardian" AND fleet='.$_SESSION["fleet"];
		$result = mysql_query($sql, $db);
		$rows = mysql_num_rows($result);
		for ($i=0; $i<$rows; $i++){
			$guardians[] = mysql_result($result, $i, "name");
		}

		$sql = 'SELECT * FROM players WHERE type LIKE "Zealot" AND fleet='.$_SESSION["fleet"];
		$result = mysql_query($sql, $db);
		$rows = mysql_num_rows($result);
		for ($i=0; $i<$rows; $i++){
			$zealots[] = mysql_result($result, $i, "name");
		}

		$i = 0;
		foreach ($zealots AS $zealot){
			if ($i >= count($guardians)) $i = 0;
			assignZealot($zealot, $guardians[$i++], $db);
		}

		echo('<meta http-equiv="refresh" content="0; URL=./">');
		exit;
	}

	if (isset($_GET['type'])) {

		$sql = "INSERT INTO players
				(name, type, fleet)
				VALUES
				('".$eve->UserName."', '".$_GET['type']."', ".$_SESSION["fleet"].")
				ON DUPLICATE KEY UPDATE
				type='".$_GET['type']."',
				fleet='".$_SESSION["fleet"]."',
				assigned=''";
		$result = mysql_query($sql, $db);

		assign($db);

		echo('<meta http-equiv="refresh" content="0; URL=./">');

	} else {
		$sql = "SELECT * FROM players WHERE name LIKE '".$eve->UserName."' AND type NOT LIKE '' AND fleet=".$_SESSION["fleet"]." ORDER BY name";
		$result = mysql_query($sql, $db);
		$rows = mysql_num_rows($result);

		if($rows == 0) {
			echo('<a href="?type=Guardian">Als Guardian anmelden</a><br>');
			echo('<a href="?type=Zealot">Als Zealot anmelden</a><br><br>');
		} else {

			$name = mysql_result($result, 0, "name");
			$curtype = mysql_result($result, 0, "type");
			$assigned = mysql_result($result, 0, "assigned");

			if ($curtype == "Zealot") {
				if (empty($assigned)) {
					echo ("You haven`t been assigned yet.<br>
							Please press the REFRESH button.<br>");
				} else {
					echo ("You are assigned to <b>$assigned</b><br>");
				}
			} else if ($curtype == "Guardian") {
				$sql = "SELECT * FROM players WHERE assigned LIKE '".$eve->UserName."' AND type NOT LIKE '' AND fleet=".$_SESSION["fleet"]." ORDER BY name";
				$result = mysql_query($sql, $db);
				$rows = mysql_num_rows($result);

				if ($rows == 0) {
					echo ("Nobody is assigned to you yet :(<br>
							Please press the REFRESH button.<br>");
				} else {
					echo ("Players assigned to you:<br>");
					for ($i=0; $i<$rows; $i++) {
						$name = mysql_result($result, $i, "name");
						$type = mysql_result($result, $i, "type");
						$assigned = mysql_result($result, $i, "assigned");

						echo ("<b>$name</b> ($type)<br>\n");
					}
				}
			}
		}

		echo("<br>Link to join/refresh the fleet:<br>\n<a href='http://unternetz.de/eve/?fleet=".$_SESSION["fleet"]."'>http://unternetz.de/eve/?fleet=".$_SESSION["fleet"]."</a><br><br>\n");

		if ($curtype != "" || $is_admin) {
			echo ("<br>\n");

			$sql = 'SELECT * FROM players WHERE type LIKE "Zealot" AND fleet='.$_SESSION["fleet"].' ORDER BY type DESC, name ASC';
			$result = mysql_query($sql, $db);
			$rows = mysql_num_rows($result);

			echo("Loggedin Zealots:<br>\n");
			echo("<table border=\"1\">\n");
			echo ("<tr><th>Zealot</th><th>Assigned To</th></tr>\n");
			for ($i=0; $i<$rows; $i++){
				$name = mysql_result($result, $i, "name");
				$type = mysql_result($result, $i, "type");
				$assigned = mysql_result($result, $i, "assigned");

				echo ("<tr><td>$name ($type)</td><td>$assigned</td></tr>\n");
			}
			echo("</table>\n");
			echo ("<br>\n");


			$sql = 'SELECT * FROM players WHERE type LIKE "Guardian" AND fleet='.$_SESSION["fleet"].' ORDER BY name ASC';
			$result = mysql_query($sql, $db);
			$rows = mysql_num_rows($result);

			echo("Loggedin Guardians:<br>\n");
			echo("<table border=\"1\">\n");
			echo ("<tr><th>Guardian</th><th>Gets From</th></tr>\n");
			$guardname = false;
			for ($i=0; $i<$rows; $i++){
				$name = mysql_result($result, $i, "name");
				$type = mysql_result($result, $i, "type");
				$assigned = mysql_result($result, $i, "assigned");

				echo ("<tr><td>$name</td><td>");

				$sql2 = 'SELECT * FROM players WHERE assigned LIKE "'.$name.'" AND fleet='.$_SESSION["fleet"].' ORDER BY name ASC';
				$result2 = mysql_query($sql2, $db);
				$rows2 = mysql_num_rows($result2);
				for ($i2=0; $i2<$rows2; $i2++){
					$name2 = mysql_result($result2, $i2, "name");
					$type2 = mysql_result($result2, $i2, "type");
					echo ("$name2 ($type2)<br>\n");
				}

				echo ("</td></tr>");
			}
			echo("</table>\n");
		}

		if ($curtype != "") {
			echo('<br><br><a href="?type=">'.$eve->UserName.' ('.$curtype.') abmelden</a>');
		}


		if (false && $is_admin) {
			echo ("<br><br><a href='?action=clear'>Clear</a><br>\n");
			echo ("<a href='?action=assign'>Assign</a> (Autoassign On)\n");

			if (isset($_GET['action'])) {
				switch($_GET['action']){
					case 'clear':
						$sql = 'TRUNCATE TABLE players';
						$result = mysql_query($sql, $db);
						echo(mysql_error());
						echo('<meta http-equiv="refresh" content="0; URL=./">');
						exit;
					case 'assign':
						assign($db);
						break;
				}
			}
		}
	}

	echo('<br><br><font size="2">Donate to TNPklon if u want to support this :)</font>');

	echo(mysql_error());

?>
</body>
</html>